package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.q.tqb.R;
import com.q.tqb.model.LevelInfo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LevelAdapter extends RecyclerView.Adapter<LevelAdapter.ViewHolder> {

    private ArrayList<LevelInfo> mRedPersonDatas;
    private Context mContext;
    private int checkePostion = -1;

    public LevelAdapter(Context context, ArrayList<LevelInfo> mRedPersonDatas) {
        this.mRedPersonDatas = mRedPersonDatas;
        this.mContext = context;

    }

    public void check(int position) {
        checkePostion = position;
        notifyDataSetChanged();
    }

    @Override
    public LevelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_level, parent, false);
        return new LevelAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final LevelAdapter.ViewHolder holder, final int position) {
        LevelInfo levelInfo = mRedPersonDatas.get(position);
        holder.tv_t.setText("LV" + levelInfo.level + ":" + levelInfo.level_name);
        holder.tv_m.setText(levelInfo.money + "元");
        if (position == checkePostion) {
            holder.relativeLayout.setBackgroundResource(R.drawable.shape_level_check);
        } else {
            holder.relativeLayout.setBackgroundResource(R.drawable.shape_level_uncheck);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mRedPersonDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_t)
        TextView tv_t;
        @BindView(R.id.tv_m)
        TextView tv_m;
        @BindView(R.id.rl)
        RelativeLayout relativeLayout;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private LevelAdapter.OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(LevelAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
